package Demo1;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.junit.Test;

public class Demo1 {
	// 无参数构造方法
	public void test1() {
		try {
			Class person = Class.forName("Demo1.Person");
			Constructor c = person.getConstructor();
			Object obj = c.newInstance(null);
			System.out.println(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 有参调用
	public void test2() {
		try {
			Class person = Class.forName("Demo1.Person");
			Constructor c = person.getConstructor(String.class);
			Object obj = c.newInstance("valuex");
			System.out.println(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 私有构造方法
	public void test3() {
		try {
			Class person = Class.forName("Demo1.Person");
			Constructor c = person.getDeclaredConstructor(int.class);
			c.setAccessible(true);// 暴力反射
			Object obj = c.newInstance(2);
			System.out.println(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 反射无参方法
	public void test4() {
		try {
			Person p = new Person();
			Class person = Class.forName("Demo1.Person");
			Method method = person.getMethod("eat", null);
			method.invoke(p, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void test5() {
		try {
			Person p = new Person();
			Class person = Class.forName("Demo1.Person");
			Method method = person.getMethod("run", String.class, String.class);
			method.invoke(p, "北京路","30号");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//返回值
	public void test6() {
		try {
			Person p = new Person();
			Class person = Class.forName("Demo1.Person");
			Method method = person.getMethod("test", String.class);
			String name=(String) method.invoke(p, "北京路");
			System.out.println(name);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//返回值 私有方法
	public void test7() {
		try {
			Person p = new Person();
			Class person = Class.forName("Demo1.Person");
			
			Method method = person.getDeclaredMethod("test2", String.class);
			method.setAccessible(true);// 暴力反射
			String name=(String) method.invoke(p, "北京路");
			System.out.println(name);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//静态方法
	public void test8() {
		try {
			Person p = new Person();
			Class person = Class.forName("Demo1.Person");
			Method method = person.getMethod("live", null);
			String name=(String) method.invoke(null,null);
			System.out.println(name);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//静态main方法
	public void test9() {
		try {
			Person p = new Person();
			Class person = Class.forName("Demo1.Person");
			Method method = person.getMethod("main", String[].class);
			 method.invoke(null,(Object)new String[]{"1","2"});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//反射类的public字段
	public void field1() {
		try {
			Person p = new Person();
			Class clazz = Class.forName("Demo1.Person");
			 Field f = clazz.getField("name");
			 f.set(p, "小狗");
			 System.out.println(p.getName());
			 System.out.println(f.get(p));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//反射类的public final字段
	public void field2() {
		try {
			Person p = new Person();
			Class clazz = Class.forName("Demo1.Person");
			Field f = clazz.getField("password");
			System.out.println(f.get(p));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//反射类的public final字段
	@Test
	public void field3() {
		try {
			Person p = new Person();
			Class clazz = Class.forName("Demo1.Person");
			Field f = clazz.getDeclaredField("age");
			f.setAccessible(true);
			f.set(p, 99);
			Integer result=(Integer) f.get(p);
			System.out.println(result);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
